//  在编码时要显示智能提示，则umcoment下面的代码,并且注释掉import * as Cesium from '../Build/CesiumUnminified/index.js'; 
//import * as Cesium from 'cesium';

//  正式运行时，下面的代码不能注释掉！
import * as Cesium from '../Build/CesiumUnminified/index.js';

// 初始化 viewer
const viewer = new Cesium.Viewer('cesiumContainer');

// 临时测试
var r1 = Cesium.Cartesian3.fromDegrees(116.397428, 39.90923, 0);
console.log(r1);

document.getElementById('flyToBeijing').onclick = () => {
  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(116.397428, 39.90923, 1000000)
  });
}

// 添加标记(city: 北京)
document.getElementById('addMarker').onclick = () => {
  const marker = new Cesium.Entity({
    position: Cesium.Cartesian3.fromDegrees(116.397428, 39.90923, 0),
    label: {
      text: "北京",
      font: '22px sans-serif',
      fillColor: Cesium.Color.WHITE,      
    },
    point: {
      pixelSize: 10,
      color: Cesium.Color.RED
    }
  });

  // 正确的添加方式
  viewer.entities.add(marker);
}

// 添加地形的功能
document.getElementById('addTerrain').onclick = async () => {
  const terrainProvider = await Cesium.CesiumTerrainProvider.fromUrl("http://localhost:8080/v1/tilesets/ldem_1024_30n_45n_090_120_lbl/tiles",
    {
      requestVertexNormals: true
    });
  viewer.terrainProvider = terrainProvider;

  // 加载特定瓦片
  try {
    const request = new Cesium.Request({
      type: Cesium.RequestType.TERRAIN,
      headers: {
        'Authorization': 'Bearer ' + Cesium.createGuid()
      }
    });

    //  实际请求http://localhost:8080/v1/tilesets/ldem_1024_30n_45n_090_120_lbl/tiles/11/3314/1410.terrain?v=1.1.0
    //  Cesium坐标原点是左上角，即Y轴向下，X轴向右
    const tile = await terrainProvider.requestTileGeometry(3314, 637, 11, request);

    if (tile) {
      console.log('瓦片数据:', {
        boundingSphere: tile.boundingSphere,
        vertices: tile.vertices,
        minimumHeight: tile.minimumHeight,
        maximumHeight: tile.maximumHeight,
        // ... 其他瓦片属性
      });
    }
  } catch (error) {
    console.error('请求瓦片失败:', error);
  }
}


// 添加地形的功能
document.getElementById('addTerrain2').onclick = async () => {
  const terrainProvider = await Cesium.CesiumTerrainProvider.fromUrl("https://marshub.s3.amazonaws.com/moon_v14",
    {
      requestVertexNormals: true
    });

  // 加载特定瓦片
  try {
    const request = new Cesium.Request({
      type: Cesium.RequestType.TERRAIN,
      headers: {
        'Authorization': 'Bearer ' + Cesium.createGuid()
      }
    });

    //  实际请求地址
    const tile = await terrainProvider.requestTileGeometry(18, 5, 4, request);

    if (tile) {
      console.log('瓦片数据:', {
        boundingSphere: tile.boundingSphere,
        vertices: tile.vertices,
        minimumHeight: tile.minimumHeight,
        maximumHeight: tile.maximumHeight,
        // ... 其他瓦片属性
      });
    }
  } catch (error) {
    console.error('请求瓦片失败:', error);
  }
}


// 卫星瓦片测试(Mb)
document.getElementById('addImagery').onclick = async () => {
    // 清空Imagery
    viewer.imageryLayers.removeAll();

    // 创建自定义 ImageryProvider
    const imageryProvider = new Cesium.UrlTemplateImageryProvider({
        url: 'https://localhost:5001/xyz/Countries/{z}/{x}/{y}.jpg',  // 替换为你的瓦片服务地址
        tilingScheme: new Cesium.WebMercatorTilingScheme(),
        minimumLevel: 0,
        maximumLevel: 6,
        // 可选参数
        credit: 'Your Credit Here',
       
    });
 
    viewer.imageryLayers.addImageryProvider(imageryProvider);
}


// 启用地形深度测试
viewer.scene.globe.enableLighting = true